package io.kiki.sba.registry.metrics;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Slf4jReporter;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import io.prometheus.client.hotspot.DefaultExports;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class ReporterUtils {

    private static final Logger logger = LoggerFactory.getLogger(ReporterUtils.class);


    public static void startSlf4jReporter(long period, MetricRegistry registry, Logger loggerMetrics) {
        Slf4jReporter reporter = Slf4jReporter.forRegistry(registry).outputTo(logger).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
        if (period > 0) {
            reporter.start(period, TimeUnit.SECONDS);
        } else {
            reporter.start(30, TimeUnit.SECONDS);
        }
    }


    public static void startSlf4jReporter(long period, MetricRegistry registry) {
        startSlf4jReporter(period, registry);
    }

    public static void enablePrometheusDefaultExports() {
        DefaultExports.initialize();
    }

    public static void registerPrometheusMetrics(MetricRegistry metrics) {
        CollectorRegistry.defaultRegistry.register(new DropwizardExports(metrics));
    }
}
